package com.bps.zzant.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.bps.zzant.model.ArtFile;
import com.bps.zzant.model.OrderGarment;
import com.bps.zzant.services.FileService;
import com.bps.zzant.services.OrderService;
import java.util.List;

@Controller
public class UploadFileController extends ZzantController{
	private static final Logger logger = LoggerFactory.getLogger(UploadFileController.class);
	
	@Autowired
	private FileService fileService;
	
	@Autowired
	private OrderService orderService;
	
	public void setFileService(FileService fileService) {
		this.fileService = fileService;
	}
	
	
	public void setOrderSerice(OrderService orderService) {
		this.orderService = orderService;
	}

	@RequestMapping(value = "/fileHome", method = RequestMethod.GET)
	public ModelAndView fileHome(HttpServletResponse response, HttpServletRequest request) {
		
		int orderId = Integer.parseInt((String)request.getParameter("orderId"));
		int customerId = Integer.parseInt(request.getParameter("customerId"));
		
		logger.info(" +++++++in UploadFileController.fileHome: customerId: " + request.getParameter("customerId"));
		logger.info(" +++++++in UploadFileController.fileHome: orderId: " + orderId);
		
//		request.getSession().setAttribute("currentCustomerId", request.getParameter("customerId"));
//		request.getSession().setAttribute("currentOrderId", request.getParameter("orderId"));
		
		List fileList = fileService.getFileListByOrderId(customerId, orderId);
		
		Iterator fileIt = fileList.iterator();
		ArtFile artFile = null;
		
		while (fileIt.hasNext()){
			logger.info(" ))))))))))))) running iterator");
			artFile =  (ArtFile)fileIt.next();
			
			File file = new File(artFile.getFileDir()+artFile.getFileName());
			
			try {
				artFile.setBytes(FileUtils.readFileToByteArray(file));
				logger.debug("/////111------- the file:" + artFile.getBytes().toString());
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			logger.debug("/////111------- the file:" + artFile.getBytes().toString());
			
		}
		
		Iterator fileIt1 = fileList.iterator();
		
		while (fileIt1.hasNext()){
			
			ArtFile artFile1 =  (ArtFile)fileIt1.next();
			
			logger.debug("/////------- the file:" + artFile1.getBytes());
			
		}
		OrderGarment order =  orderService.getOrderById(orderId);
		Map map = new HashMap();
		map.put("order", order);
		map.put("fileList", fileList);
		
		return new ModelAndView("filehome", map);
	}
	

	
}
